package com.sg.business.commons.ui.work;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;

import com.mobnut.db.model.IContext;
import com.mobnut.db.model.PrimaryObject;
import com.sg.business.model.Work;

public abstract class ProjectChangePanel extends Composite {

	private PrimaryObject input;
	private boolean changeOrg;
	private boolean changeBudget;
	private boolean changePlan;
	private boolean changeWork;
	private boolean isEditable;

	public ProjectChangePanel(Composite parent, int style) {
		super(parent, style);
	}

	public void createContent() {
		setLayout(new FormLayout());

		Composite bottom = null;

		bottom = createProjectInfo(bottom);

		bottom = createWBS(bottom);

		bottom = createProjectBudget(bottom);
	}

	private Composite createProjectBudget(Composite bottom) {
		if (changeBudget) {
			ProjectChangeBudgetCreater pcb = new ProjectChangeBudgetCreater(
					this, SWT.NONE);
			pcb.setEditable(isEditable);
			pcb.setContext(getContext());
			pcb.setInput(input);
			pcb.createContent();

			FormData fd = new FormData();
			pcb.setLayoutData(fd);

			if (bottom != null) {
				fd.top = new FormAttachment(bottom, 2);
			} else {
				fd.top = new FormAttachment(0, -2);
			}
			fd.left = new FormAttachment(0, 2);
			fd.right = new FormAttachment(100, -2);
			fd.height = 300;
			return pcb;
		}
		return null;
	}

	private Composite createWBS(Composite bottom) {
		if (changeOrg || changeWork || changePlan) {
			ProjectChangeWBSCreater pcw = new ProjectChangeWBSCreater(this,
					SWT.NONE);
			pcw.setChangeOrg(changeOrg);
			pcw.setChangeWork(changeWork);
			pcw.setChangePlan(changePlan);
			pcw.setEditable(isEditable);
			pcw.setContext(getContext());
			pcw.setInput(input);
			pcw.createContent();

			FormData fd = new FormData();
			pcw.setLayoutData(fd);

			if (bottom != null) {
				fd.top = new FormAttachment(bottom, 2);
			} else {
				fd.top = new FormAttachment(0, -2);
			}
			fd.left = new FormAttachment(0, 2);
			fd.right = new FormAttachment(100, -2);
			fd.height = 300;
			return pcw;
		}
		return null;
	}

	private Composite createProjectInfo(Composite bottom) {
		if (changeOrg || changePlan) {
			ProjectChangeInfoCreater pci = new ProjectChangeInfoCreater(this,
					SWT.NONE);
			pci.setChangeOrg(changeOrg);
			pci.setChangePlan(changePlan);
			pci.setEditable(isEditable);
			pci.setContext(getContext());
			pci.setInput(input);
			pci.createContent();

			FormData fd = new FormData();
			pci.setLayoutData(fd);

			if (bottom != null) {
				fd.top = new FormAttachment(bottom, 2);
			} else {
				fd.top = new FormAttachment(0, -2);
			}
			fd.left = new FormAttachment(0, 2);
			fd.right = new FormAttachment(100, -2);

			return pci;
		}
		return null;
	}

	public void setInput(PrimaryObject data) {
		this.input = data;
		initData();
	}

	private void initData() {
		if (input instanceof Work) {
			Work work = (Work) input;
			List<?> changeTypeList = work
					.getListValue(Work.F_PROJECTCHANGE_TYPE);
			if (changeTypeList.contains("org")) {
				this.changeOrg = Boolean.TRUE;
			} else {
				this.changeOrg = Boolean.FALSE;
			}
			if (changeTypeList.contains("budget")) {
				this.changeBudget = Boolean.TRUE;
			} else {
				this.changeBudget = Boolean.FALSE;
			}
			if (changeTypeList.contains("plan")) {
				this.changePlan = Boolean.TRUE;
			} else {
				this.changePlan = Boolean.FALSE;
			}
			if (changeTypeList.contains("work")) {
				this.changeWork = Boolean.TRUE;
			} else {
				this.changeWork = Boolean.FALSE;
			}

		}
	}

	protected abstract IContext getContext();

	public void setEditable(boolean isEditable) {
		this.isEditable = isEditable;
	}

}
